<template>
  <div>
    <el-card shadow="hover" style="text-align: left;margin-bottom: 10px">
      <div class="header">
        <div class="avatar-area">
          <el-image class="avatar" :src="userInfo.avatar" :preview-src-list="[userInfo.avatar]"></el-image>
          <div class="v_item_bottom" >
            <i class="el-icon-edit v_cursor"   @click.stop="showAvatarPop"></i>
          </div>
        </div>
        <div class="info">
          <h1 class="nickname">{{ userInfo.nickname }} <span class="change-password"
                                                             @click="showChangePasswordPop()">修改密码</span>
          </h1>
        </div>
      </div>
    </el-card>
    <el-card shadow="hover">
      <h1 class="el-card__header" style="text-align: left;margin: 0">基本信息</h1>
      <el-form :data="fromData" label-width="100px" label-position="left"
               style="text-align: left;width: 80%;margin-left: 30px;margin-top: 20px">
        <el-form-item label="用户角色">
          <div style="margin-left: 10px" v-if="fromData.type == 0">普通用户</div>
          <div style="margin-left: 10px" v-else-if="fromData.type == 1">管理员</div>
          <div style="margin-left: 10px" v-else-if="fromData.type == 2">超级管理员</div>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="fromData.nickname"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="fromData.email"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="handleEditUserInfo">提交修改</el-button>
    </el-card>
    <SingeUploadDialog :is-show.sync="isShowAvatarPop" :info="avatarInfo" @complete="onComplete"></SingeUploadDialog>
    <ChangePasswordDialog :is-show.sync="isShowChangePasswordPop"></ChangePasswordDialog>
  </div>

</template>

<script>
import { mapState } from 'vuex'
import SingeUploadDialog from '@/components/SingeUploadDialog.vue'
import { reqEditUserInfo } from '@/api/clientApi'
import ChangePasswordDialog from '@/views/admin/dialog/ChangePasswordDialog.vue'

export default {
  components: { ChangePasswordDialog, SingeUploadDialog },
  data() {
    return {
      fromData: {
        nickname: '',
        email: ''
      },
      isShowAvatarPop: false,
      isShowChangePasswordPop: false,
      avatarInfo: {},
      imageUrl: '',
    }
  },
  computed: {
    ...mapState('user', [
      'userInfo'
    ])

  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      this.fromData.nickname = this.userInfo.nickname;
      this.fromData.email = this.userInfo.email;
    },
    showAvatarPop() {
     // this.avatarInfo = { imageUrl: this.userInfo.avatar }
      this.avatarInfo = {  }
      this.isShowAvatarPop = true
    },
    showChangePasswordPop() {
      this.isShowChangePasswordPop=true
    },
    handleEditUserInfo() {
      this.handleEditUser(this.fromData)
    },
    onComplete(value) {
      this.handleEditUser({ avatar: value })
    },
    handleEditUser(params) {
      reqEditUserInfo(params).then(data => {
        this.$store.dispatch('user/getUserInfo')
        this.isShowAvatarPop = false
        this.$message.success("修改成功")
      })
    }
  }
}
</script>

<style scoped lang="stylus">
.avatar {
  width: 100px;
  height: 100px;
  border: 2px solid white;
  box-shadow: 0 0 10px 2px rgba(0, 0, 0, .06);
  border-radius: 50%;
  box-sizing: border-box;
}


.camera {
  width: 35px;
  height: 35px;
  border-radius: 50%;
}

.header {
  display: flex;
  justify-content: flex-start;

  .avatar-area {
    width: 100px;
    position: relative;
    margin-right: 20px;
  }
}

.change-password {
  color: #3a8ee6;
  font-size: 12px;
  margin-left: 10px;
  font-weight: normal;
}

.change-password:hover {
  cursor: pointer;
}

.avatar-area {
  position relative
  width 100px
  height 100px

  &:hover {
    .v_item_bottom {
      opacity 1;
      display flex
      flex-direction row
    }
  }

  .v_item_bottom {
   // pointer-events: none;
    width 100%
    height 32px
    position absolute
    bottom 0
    left 0
    background: rgba(0, 0, 0, 0.5);
    border-radius: 0px 0px 4px 4px;
    z-index 999
    display flex
    flex-direction row
    align-items center
    justify-content center
    opacity 0
    transition opacity 0.3s

    img {
      width 14px
      height 14px
    }

    i {
      color white
      font-size 18px
    }

    p {
      margin-left 5px
      font-size: 12px;
      font-family: MicrosoftYaHei;
      color: #FFFFFF;
    }
  }
}
</style>
